import { createStore } from 'vuex'

export default createStore({
  state: {
    token: localStorage.getItem('token') ? localStorage.getItem('token') : null,
    currentMenu : null,
    tabList:[
      {
        path: "/admin",
        name: 'AdHome',
        label: '首页',
        url: ''
      }
    ],
  },
  mutations: {
    setToken(state, token) {
      localStorage.setItem('token', token)
      state.token = token
    },
    logout(state) {
      localStorage.removeItem('token')
      state.token = null
    },
    selectMenu(state, val) {
      if (val.name === 'AdHome') {
        state.currentMenu = null
      } else {
        state.currentMenu = val
        //    新增tabList
        let result = state.tabList.findIndex(item => item.name === val.name)
        result === -1 ? state.tabList.push(val) : ''
      }
    },
    // 删除页面标签的函数
    closeTag(state,val) {
      let result = state.tabList.findIndex(item => item.name === val.name)
      state.tabList.splice(result,1)
    }
  },
  actions: {
  },
  modules: {
  }
})
